<?php

namespace app\common\model\mall\shop;

use think\Model;
use app\common\model\mall\oper\Area;

class Freightprice extends Model
{
    // 表名
    protected $name = 'mall_shop_freightprice';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;

    protected $append = [
        'freight_id_text',
        'express_ids_text',
        'region_ids_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getFreightIdTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['freight_id']) ? $data['freight_id'] : '');
        $names = db("mall_shop_freight")->where("id",$value)->column("name");
        return $names ? $names : '';
    }

    public function getExpressIdsTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['express_ids']) ? $data['express_ids'] : '');
        return !$value ? "" : db("mall_oper_express")->where("id","in", $value)->column("name");
    }

    public function getRegionIdsTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['region_ids']) ? $data['region_ids'] : '');
        return !$value ? "" : db("area")->where("id","in", $value)->column("name");
    }
}
